package com.jdk.learn.juc2.list;

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 类功能描述:
 *      CopyOnWriteArrayList的弱一致性问题
 * @author bgt
 * @version V1.0
 * @since 2019-02-15 15:14
 */
public class CopyList {
    private static volatile CopyOnWriteArrayList<String> COWlist=new CopyOnWriteArrayList<String>();

    public static void main(String[] args) throws InterruptedException {
        COWlist.add("hello");
        COWlist.add("ricky");
        COWlist.add("welcome");
        COWlist.add("guahao");
//        COWlist.add("guahao2");
        System.out.println(COWlist.indexOf("guahao"));
        Thread thread1=new Thread(()->{
            COWlist.set(1, "ricky-shinaizi");
            COWlist.remove(2);
            COWlist.remove(3);
        });
        Iterator<String> iterator = COWlist.iterator();
        Thread.sleep(10);
        thread1.start();
        thread1.join();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}
